package com.shenyue.nbtownlet.model;

import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.util.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;

public class HttpClientExManager {
    private static HttpClientExManager instance;

    protected HttpClientExManager() {
    }

    public static HttpClientExManager instance() {
        if (instance == null) {
            instance = new HttpClientExManager();
        }
        return instance;
    }

    public RequestCall get(String server, final String action, JSONObject param, final BaseListener listener) {
        try {
            GetBuilder builder = OkHttpUtils.get();
            builder.url(server);
            if (param != null) {
                Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    String value = StringUtil.getValue(entry.getValue());
                    builder.addParams(entry.getKey(), value);
                }
            }

            RequestCall call = builder.build();
            call.execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    //String message = e.getMessage();
                    listener.error("发送网络请求出错..., 请稍候再试", action);
                }

                @Override
                public void onResponse(String response, int id) {
                    try {
                        listener.success(response, action);
                    } catch (Exception e) {
                        listener.error("数据解析出错！", action);
                    }
                }
            });
            return call;
        } catch (Exception e) {
            listener.error(e.getMessage(), action);
        }
        return null;
    }

    public RequestCall post(String server, final String action, JSONObject param, final BaseListener listener) {
        try {
            PostFormBuilder builder = OkHttpUtils.post();
            builder.url(server);
            if (param != null) {
                Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> entry = it.next();
                    String value = StringUtil.getValue(entry.getValue());
                    builder.addParams(entry.getKey(), value);
                }
            }

            RequestCall call = builder.build();
            call.execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    //String message = e.getMessage();
                    listener.error("发送网络请求出错..., 请稍候再试", action);
                }

                @Override
                public void onResponse(String response, int id) {
                    try {
                        listener.success(response, action);
                    } catch (Exception e) {
                        listener.error("数据解析出错！", action);
                    }
                }
            });
            return call;
        } catch (Exception e) {
            listener.error(e.getMessage(), action);
        }
        return null;
    }
}
